#include "SdkMotionDetect.h"

#define DEF_TAG             "MD"

StSdkMDAreaLocal g_SdkMotionDetect_Area;

int Ext_SdkMotionDetect_Init(void)
{
    int sensitivity = 30;
    sensitivity = 80;
    int ret = AK_FAILED;

    memset(&g_SdkMotionDetect_Area,0,sizeof(g_SdkMotionDetect_Area));

    /*
    * step 1:md init
    * default parameters: detect in 10 fps
    */
    if (AK_SUCCESS != ak_md_init(VIDEO_DEV0))
    {
        ak_print_normal(MODULE_ID_APP, "dev %d md init fail\n", VIDEO_DEV0);
        return ret;
    }

    /*
    * step 2:set sensitivity
    */
    if (AK_SUCCESS != ak_md_set_sensitivity(VIDEO_DEV0, sensitivity))
    {
        ak_print_normal(MODULE_ID_APP, "dev %d set_sensitivity fail\n", VIDEO_DEV0);
        return ret;
    }

    /*
    * step 3:set area
    */
    /** v is y,h is x **/
    char area_mask[VPSS_MD_DIMENSION_V_MAX * VPSS_MD_DIMENSION_H_MAX] = {0};
    int v = 0;
    int h = 0;
    int v_begin = 0;
    int h_begin = 0;
    int v_end = VPSS_MD_DIMENSION_V_MAX;
    int h_end = VPSS_MD_DIMENSION_H_MAX;

    v_begin = 17;
    h_begin = 6;
    v_end = VPSS_MD_DIMENSION_V_MAX;
    h_end = VPSS_MD_DIMENSION_H_MAX - 6;

    for (v = v_begin; v < v_end; v++)
    {
        for(h = h_begin; h < h_end; h++)
        {
            area_mask[v * VPSS_MD_DIMENSION_H_MAX + h] = 1;
        }
    }

    if(v_begin > 0 || h_begin > 0 || v_end != VPSS_MD_DIMENSION_V_MAX || h_end != VPSS_MD_DIMENSION_H_MAX)
    {
        if (AK_SUCCESS != ak_md_set_area(VIDEO_DEV0, area_mask))
        {
            ak_print_normal(MODULE_ID_APP, "dev %d set_area_sensitivity fail\n", VIDEO_DEV0);
            return ret;
        }
    }

    /*
    * step 4:md enable
    */
    if (AK_SUCCESS != ak_md_enable(VIDEO_DEV0, AK_TRUE))
    {
        ak_print_normal(MODULE_ID_APP, "dev %d md_enable fail\n", VIDEO_DEV0);
        return ret;
    }

    return 0;
}

void Ext_SdkMotionDetect_Exit(void)
{
    ak_md_destroy(VIDEO_DEV0);
    ak_print_normal(MODULE_ID_APP, "*** md exit ***\n");
}

int Ext_SdkMotionDetect_SetSensitivity(int val)
{
    if (AK_SUCCESS != ak_md_set_sensitivity(VIDEO_DEV0, val))
    {
        ak_print_normal(MODULE_ID_APP, "dev %d set_sensitivity fail\n", VIDEO_DEV0);
        return -1;
    }

    return 0;
}

void Ext_SdkMediaCom_GetMDAreaXY(pStSdkMDAreaLocal area)
{
    area->l = g_SdkMotionDetect_Area.l;
    area->r = g_SdkMotionDetect_Area.r;
    area->t = g_SdkMotionDetect_Area.t;
    area->b = g_SdkMotionDetect_Area.b;
}

int Ext_SdkMediaCom_GetMdResult(void)
{
    struct md_result_info ret_info;

    if(AK_SUCCESS == ak_md_get_result(VIDEO_DEV0, &ret_info, 0) && (ret_info.result > 0))
    {
        g_SdkMotionDetect_Area.l = ret_info.boxes[0].left;
        g_SdkMotionDetect_Area.r = ret_info.boxes[0].right;
        g_SdkMotionDetect_Area.t = ret_info.boxes[0].top;
        g_SdkMotionDetect_Area.b = ret_info.boxes[0].bottom;
        // PRT_INFO(DEF_TAG, "t: %d,b: %d,l: %d,r: %d\n",
        //          ret_info.boxes[0].top, ret_info.boxes[0].bottom,
        //          ret_info.boxes[0].left, ret_info.boxes[0].right);
        return DEF_COM_TRUE;
    }
    else if(AK_SUCCESS != ak_md_get_result(VIDEO_DEV0, &ret_info, 0))
    {
        return DEF_COM_FALSE;
    }
    else
    {
        return DEF_COM_FALSE;
    }
}
